#include "jiguang.h"

namespace fireplane
{
	JiGuangBullet::JiGuangBullet():
		_is_finish(false)
	{

	}

	JiGuangBullet::~JiGuangBullet(){

	}

	void JiGuangBullet::init(Bullet::BulletCommand* cmd){
		_speed = cmd->_speed;
		_dir = cmd->_dir;
		_owner = cmd->o;

		char buffer[0xff];
		sprintf(buffer,"%s","bullet/npc6/3.png");
		_body = CCSprite::create(buffer);
		_body->setAnchorPoint(ccp(0.5f,0.0f));

		float degree = CC_RADIANS_TO_DEGREES(acos(ccpDot(_dir,ccp(0,1))));

		if (ccpCross(_dir,ccp(0,1)) < 0)
		{
			degree = 360 - degree;
		}

		_body->setRotation(degree);

		_all_bullets_parent->addChild(this);
		this->addChild(_body);
		this->setPosition(cmd->_pos);

		_startPos = cmd->_pos;
		_startAngle = 0;

		CCScaleTo* s0 = CCScaleTo::create(0.2f,1.0f,1.5f);
		CCCallFunc* call = CCCallFunc::create(this,callfunc_selector(JiGuangBullet::scale2Long));
		CCDelayTime* delay = CCDelayTime::create(2.0f);
		CCCallFunc* call1 = CCCallFunc::create(this,callfunc_selector(JiGuangBullet::finish));
		CCSequence* seq = CCSequence::create(s0,call,delay,call1,NULL);
		_body->runAction(seq);

		CCTintTo* t0 = CCTintTo::create(0.1f,200,200,200);
		CCTintTo* t1 = CCTintTo::create(0.1f,255,255,255);
		CCSequence* seq1 = CCSequence::create(t0,t1,NULL);
		CCRepeatForever* action = CCRepeatForever::create(seq1);
		_body->runAction(action);
	}

	void JiGuangBullet::finish(){
		_is_finish = true;
	}

	void JiGuangBullet::scale2Long(){
		_body->setScaleY(100.0f);
	}

	bool JiGuangBullet::updateState(float dt){
		if (isDestroy())
		{
			return false;
		}

		CCSize winSize = CCDirector::sharedDirector()->getWinSize();
		CCRect rect = CCRectMake(0,0,winSize.width,winSize.height);

		CCPoint pos = this->getPosition();
		CCPoint worldpos = this->getParent()->convertToWorldSpace(pos);

		if (rect.containsPoint(worldpos))
		{
// 			CCPoint move;
// 			move.x = _speed * dt * _dir.x;
// 			move.y = _speed * dt * _dir.y;
// 
// 			CCPoint curpos = this->getPosition();
// 			curpos.x += move.x;
// 			curpos.y += move.y;
// 			this->setPosition(curpos);

			return !_is_finish;
		}
		else
		{
			// out of screen
			return false;
		}
	}
}